import * as vscode from 'vscode';
import * as WORKSPACE_UTILS from './workspace';
import { platformCmd, registerBasic, outputChannel } from './common';

/**
 * 安装依赖~
 */
const installPack = (): void => {
	try {
		outputChannel.appendLine(`🚀 ~开始安装依赖`);
		let terminal = WORKSPACE_UTILS.getTerminalOpened()?.[0];
		if (!terminal) {
			terminal = vscode.window.createTerminal(`wit-helper`);
		}
		terminal.sendText(`${platformCmd} ${WORKSPACE_UTILS.getRootPath()}`); // 切换到工作区目录
		terminal.sendText(`npm install prettier eslint -D`); // 安装依赖并保存到package.json
		terminal.sendText(`npm install`); // 安装依赖并保存到package.json
		terminal.show();
	} catch (error: any) {
		vscode.window.showErrorMessage(`安装依赖失败：${error.message}`);
	}
};

/**
 * TODO 安装插件
 * vetur,vue.js Extension Pack, 中文包, Auto Close Tag,
 * Auto Complete Tag,auto Important, Auto Rename Tag, Chinese(Simplified),
 * Code Spell Checker, CSS Peek, EditorConfig for VS Code, GitLens — Git supercharged,
 * IntelliCode,
 */
const installExtension = (): void => {};

/**
 * 覆写setting.json
 */
const overrideSetting = async (): Promise<void> => {
	outputChannel.appendLine(`🚀 ~初始化vsCode配置: Start`);
	const settingCfg = require('./config/vs-setting.json');
	for (const [key, value] of Object.entries(settingCfg)) {
		const currentCfg = vscode.workspace.getConfiguration();
		await currentCfg.update(key, value, true); // 第三个参数为是否需要同步更新到所有工作区
	}
	outputChannel.appendLine(`🚀 ~初始化vsCode配置: End`);
};

/**
 * 注册初始化命令
 */
export const register_Command_Init = (): vscode.Disposable => {
	return registerBasic('command:init', () => {
		installPack();

		// installExtension();

		overrideSetting();
	});
};

/**
 * 注册所有命令
 * @param context
 */
export const register = (context: vscode.ExtensionContext): void => {
	// 初始化项目依赖
	context.subscriptions.push(register_Command_Init());
};
